<?php

namespace app\extra\H5WeixinSDK;

class WxOpenid {

    /**
     * 配置参数
     *
     * @var array
     */
    protected $_config = array();

    function __construct() {
        $this->_config = array(
            'APPID' => WxConfig::APPID,
            'APPSERCERT' => WxConfig::APPSERCERT,
            'BACKURL' => "https://xf2.qiu7.net/api/quicklogin/getwxuserinfo",
        );
    }

    /**
     *  获取CODE,不在微内核
     *
     */
    function getcode($backurl) {
        $url = "https://open.weixin.qq.com/connect/qrconnect?appid=" . $this->_config['APPID'] .
               "&redirect_uri=" . $backurl . "&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect";
 
        header('Location: ' . $url);
        exit;
    }

    /**
     *  获取CODE 在微信内核
     *
     */
    function getcodewx($backurl, $component_appid = '') {
        if (!empty($component_appid)) {
            $component_appid = '&component_appid=' . $component_appid;
        }
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->_config['APPID'] .
                "&redirect_uri=" . $backurl . "&response_type=code&scope=snsapi_userinfo&state=STATE" . $component_appid . "#wechat_redirect";
 
        header('Location: ' . $url);
        exit;
    }

    /**
     * 通过CODE获取TOKEN
     * 
     */
    function gettokenbycode($code) {
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->_config['APPID'] .
                "&secret=" . $this->_config['APPSERCERT'] . "&code=" . $code . "&grant_type=authorization_code"; 
      
        return file_get_contents($url); //$this->curlpost($url,'','GET');	
    }

    /**
     * 刷新或续期access_token使用
     * 
     */
    function getrefreshtoken($refresh_token) {
        $url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" . $this->_config['APPID'] .
                "&grant_type=refresh_token&refresh_token=" . $refresh_token;
        return $this->curlpost($url); //$this->curlpost($url,'','GET');
    }

    /**
     * 检验授权凭证（access_token）是否有效
     *
     */
    function invalidaccesstoken($access_token, $openid) {
        $url = "https://api.weixin.qq.com/sns/auth?access_token=" . $access_token . "&openid=" . $openid;
        return $this->curlpost($url, '', 'GET');
    }

    /**
     * 获取用户个人信息
     *
     */
    function getuserinfo($access_token, $openid) {
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid;
        return $this->curlpost($url, '', 'GET');
    }

    /**
     * 获取服务component_access_token
     *
     */
    function component_access_token($ticket) {
        $url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
        $post = json_encode(array('component_appid' => $this->_config['component_appid'], 'component_appsecret' => $this->_config['component_appsecret'], 'component_verify_ticket' => $ticket));

        return $this->curlpost($url, $post, 'POST');
    }

    /**
     * 获取服务api_create_preauthcode
     *
     */
    function api_create_preauthcode($component_access_token) {
        $url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" . $component_access_token;
        $post = json_encode(array('component_appid' => $this->_config['component_appid']));

        return $this->curlpost($url, $post, 'POST');
    }

    function curlpost($url, $post = '', $type = 'POST') {
        $ch = curl_init(); //初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        if ($type == 'POST') {
            curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }

        $data = curl_exec($ch); //运行curl
        curl_close($ch);
        return $data;
    }

}
